package cn.backflow.generator.util;

import java.sql.Timestamp;

/**
 * @author badqiu
 */
public class TestDataGenerator {

    private static boolean isNumberType(String javaType) {
        javaType = javaType.toLowerCase();
        return javaType.contains("byte")
                || javaType.contains("short")
                || javaType.contains("integer")
                || javaType.contains("int")
                || javaType.contains("long")
                || javaType.contains("double")
                || javaType.contains("bigdecimal")
                || javaType.contains("float");
    }

    public String getDBUnitTestData(String columnName, String javaType, int size) {
        if (size <= 0) size = 2;

        int MAX_SIZE = 3;
        if (javaType.contains("Boolean")) {
            return "0";
        }
        if (javaType.contains("Timestamp")) {
            return new Timestamp(System.currentTimeMillis()).toString();
        }
        if (javaType.contains("java.sql.Date")) {
            return new java.sql.Date(System.currentTimeMillis()).toString();
        }
        if (javaType.contains("java.sql.Time")) {
            return new java.sql.Time(System.currentTimeMillis()).toString();
        }
        if (javaType.contains("java.util.Date")) {
            return new Timestamp(System.currentTimeMillis()).toString();
        }
        if (javaType.contains("String")) {
            if (size > columnName.length()) {
                int tempSize = Math.min(size - columnName.length(), MAX_SIZE);
                return columnName + StringHelper.randomNumeric(tempSize);
            }
            return StringHelper.randomNumeric(Math.min(size, MAX_SIZE));
        }
        if (isNumberType(javaType)) {
            return StringHelper.randomNumeric(Math.min(size, MAX_SIZE));
        }
        return "";
    }

}
